package com.miao.im.api;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.miao.im.common.ApiResult;
import com.miao.im.common.Constant;
import com.miao.im.common.annotation.ApiAuth;
import com.miao.im.dto.AuthRequestDto;
import com.miao.im.dto.UserRespDto;
import com.miao.im.service.auth.IAuthManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/4/12 21:06
 * @Description:
 */
@RestController
@RequestMapping("/auth")
@Slf4j
public class AuthController {
    @Autowired
    private IAuthManager<UserRespDto> authManager;
    @Autowired
    private HttpServletRequest request;

    @PostMapping("/register")
    @ApiAuth(needAuth = false)
    public ApiResult<UserRespDto> register(@RequestBody @Validated AuthRequestDto authRequestDto) {
        UserRespDto respDto = authManager.register(authRequestDto);
        return ApiResult.ok(respDto);
    }

    @PostMapping("/login")
    @ApiAuth(needAuth = false)
    public ApiResult<UserRespDto> login(@RequestBody @Validated AuthRequestDto authRequestDto) {
        UserRespDto respDto = authManager.login(authRequestDto);
        return ApiResult.ok(respDto);
    }

    @PostMapping("/logout")
    @ApiAuth(needAuth = false)
    public ApiResult<?> logout() {
        String deviceNumber = request.getHeader(Constant.WEB_DEVICE_NUMBER_HEADER);
        Assert.notBlank(deviceNumber,"设备号缺失！");
        String sessionId = request.getHeader(Constant.WEB_SESSION_HEADER);
        if (StrUtil.isNotBlank(sessionId)) {
            authManager.logout(sessionId, deviceNumber);
        }
        return ApiResult.ok(null);
    }

    /**
    * @Description: 初始化信息（打开app后调用此接口）
    * @Author: miaoguoxin
    * @Date: 2021/4/28
    */
    @PostMapping("/init")
    public ApiResult<?> init(){
        String deviceNumber = request.getHeader(Constant.WEB_DEVICE_NUMBER_HEADER);
        String sessionId = request.getHeader(Constant.WEB_SESSION_HEADER);
        authManager.refreshLoginTime(sessionId,deviceNumber);
        return ApiResult.ok(null);
    }

    @RequestMapping("/test")
    @ApiAuth(needAuth = false)
    public ApiResult<?> test(@RequestBody Map<String,Object> params) throws InterruptedException {
        if (params.get("error") !=null){
            throw new RuntimeException("test");
        }
        if (params.get("time")!=null){
            Thread.sleep(3000);
        }
        return ApiResult.ok(null);
    }

    @PostMapping("/hook")
    @ApiAuth(needAuth = false)
    public ApiResult<?> testHook(@RequestBody List<Object> params){
        log.info("回调Hook：{}",JSON.toJSONString(params));
        return ApiResult.ok(null);
    }

    private static String decode(String base64text) {
        return new String(Base64.getDecoder().decode(base64text), StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        System.out.println(decode("1_YTQwZGE5ZmRjNmFmNDlmOWEyNjJkYzEwYTllOGUyM2VAMTkyLjE2OC4xLjk="));
    }
}
